# frozen_string_literal: true

module VariableTypeSeed
  def self.run
    create_string

    create_integer

    create_boolean

    create_float

    create_array

    create_hash
  end

  def self.create_string
    type = VariableType.create!(
      name: 'string',
      klass: 'String',
      status: VariableType::Status::DEPLOYED
    )

    spec_methods = [
      {
        name: 'concat',
        parameters: '{"other": {"required": true}}',
        description: '连接字符串'
      },
      {
        name: 'empty?',
        parameters: '[]',
        description: '比较字符串是否为空'
      }
    ]

    type.type_methods.create!(general_methods + compare_methods + spec_methods)
  end

  def self.create_integer
    type = VariableType.create!(
      name: 'integer',
      klass: 'Integer',
      status: VariableType::Status::DEPLOYED
    )

    spec_methods = []

    type.type_methods.create!(general_methods + calc_methods + compare_methods + spec_methods)
  end

  def self.create_float
    type = VariableType.create!(
      name: 'float',
      klass: 'Float',
      status: VariableType::Status::DEPLOYED
    )

    spec_methods = [
      {
        name: 'floor',
        parameters: '[]',
        description: '向下取整'
      },
      {
        name: 'ceil',
        parameters: '[]',
        description: '向上取整'
      },
      {
        name: 'round',
        parameters: '[]',
        description: '四舍五入'
      }
    ]

    type.type_methods.create!(general_methods + calc_methods + compare_methods + spec_methods)
  end

  def self.create_boolean
    type = VariableType.create!(
      name: 'boolean',
      klass: 'Custom::Boolean',
      status: VariableType::Status::DEPLOYED
    )

    spec_methods = []

    type.type_methods.create!(general_methods + spec_methods)
  end

  def self.create_array
    type = VariableType.create!(
      name: 'array',
      klass: 'Array',
      status: VariableType::Status::DEPLOYED
    )

    spec_methods = [
      {
        name: 'include?',
        parameters: '[{"name", "element", "required": true}]',
        description: '是否包含子元素'
      }
    ]

    type.type_methods.create!(general_methods + spec_methods)
  end

  def self.create_hash
    type = VariableType.create!(
      name: 'hash',
      klass: 'Hash',
      status: VariableType::Status::DEPLOYED
    )

    spec_methods = [
      {
        name: 'keys?',
        parameters: '[{"name", "key", "required": true}]',
        description: '判读这个key是否存在'
      }
    ]

    type.type_methods.create!(general_methods + spec_methods)
  end

  def self.general_methods
    @general_methods ||= [
      {
        name: 'nil?',
        parameters: '[]',
        description: '判断是否为nil'
      },
      {
        name: 'eql?',
        parameters: '[{"name":"other","required": true}]',
        description: '判断是否为nil'
      },
      {
        name: '=',
        parameters: '[{"name":"other","required": true}]',
        description: '判断是否为nil'
      },
      {
        name: '!=',
        parameters: '[{"name":"other","required": true}]',
        description: '判断是否为nil'
      }
    ]
  end

  def self.calc_methods
    @calc_methods ||= [
      {
        name: '+',
        parameters: '[{"name":"other","required": true}]',
        description: '相加'
      },
      {
        name: '-',
        parameters: '[{"name":"other","required": true}]',
        description: '相减'
      },
      {
        name: '*',
        parameters: '[{"name":"other","required": true}]',
        description: '乘'
      },
      {
        name: '/',
        parameters: '[{"name":"other","required": true}]',
        description: '除'
      }
    ]
  end

  def self.compare_methods
    @compare_methods ||= [
      {
        name: '<=>',
        parameters: '[{"name":"other","required": true}]',
        description: '比较'
      },
      {
        name: '>',
        parameters: '[{"name":"other","required": true}]',
        description: '大于'
      },
      {
        name: '>=',
        parameters: '[{"name":"other","required": true}]',
        description: '大于等于'
      },
      {
        name: '<',
        parameters: '[{"name":"other","required": true}]',
        description: '小于'
      },
      {
        name: '<=',
        parameters: '[{"name":"other","required": true}]',
        description: '小于等于'
      }
    ]
  end
end
